package com.jason.kotlin.gank.viewmodel

import android.app.Application
import androidx.lifecycle.viewModelScope
import com.jason.kotlin.gank.model.RollJokeBean
import com.jason.kotlin.gank.repository.RollApiImpl
import com.jason.library.common.base.BasePageViewModel
import com.jason.library.common.base.DataWrapper
import kotlinx.coroutines.launch
import java.lang.Exception

/**
 * @description: RollJokeViewModel
 *
 * @author ChenBaoyang
 * @date 4/25/21 13:59
 */
class RollJokeViewModel(application: Application) : BasePageViewModel<RollJokeBean>(application) {

    private val rollApi = RollApiImpl()

    override fun loadData(page: Int) {
        viewModelScope.launch {
            showLoading()
            try {
                val response = rollApi.getJokeList(page)
                data.postValue(DataWrapper.success(response.data.list))

                showContent()
            }catch (e: Exception) {
                showError(e.toString())
            }
        }

    }
}